﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ProductManager_AddDiscontPrdts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Master.ElementSelectedChange = GetTreeView;
            SetDLLType();
            GetPrdtDataByType(extGVPromotedPrdt, null);
            GetProList();
            //GetDiscountOrRecommandedGV(extGVProPrdt);
        }
    }
    #region Initial Object
    GoShopping.BLL.PrdctInfo.DiscountPrdctBean dpb = new GoShopping.BLL.PrdctInfo.DiscountPrdctBean();
    GoShopping.BLL.PrdctInfo.RecomendPrdctBean rpb = new GoShopping.BLL.PrdctInfo.RecomendPrdctBean();
    GoShopping.Model.DiscountPrdct dp = new GoShopping.Model.DiscountPrdct();
    GoShopping.Model.RecomendPrdct rp = new GoShopping.Model.RecomendPrdct();
    GoShopping.BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
    GoShopping.Model.Product prdt = new GoShopping.Model.Product();
    #endregion

    #region Common Method
    public string GetPrdtName(object obj)
    {
        GoShopping.BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
        return pb.GetProduct(Convert.ToInt32(obj)).Name;
    }
    private void GetTreeView()
    {
        TreeView tv = Master.MasterTreeView;
        SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("Navigation.xml"), "prdt");
    }

    void GetPrdtDataByType(GridView gv, string filterString)
    {
        if (string.IsNullOrEmpty(filterString))
        {
            gv.DataSource = pb.GetAllProduct();
            gv.DataBind();
        }
        else
        {
            gv.DataSource = pb.GetProductList(filterString);
            gv.DataBind();
        }
    }

    void GetDiscountOrRecommandedGV(GridView gv)
    {
        if (gv.ID == "extGVProPrdt")
        {
            gv.DataSource = dpb.GetAll();
            gv.DataBind();
        }
        else if (gv.ID == "extGVRecommandedPrdt")
        {
            gv.DataSource = rpb.GetAll();
            gv.DataBind();
        }
        else if (gv.ID == "extGVMainPrdt")
        {
            //gv.DataSource = dpb.GetAll();
        }
    }

    void SetDLLType()
    {
        SetControlCommon.GetDDLType(ddlType);
    }

    public string GetSupplierName(object obj)
    {
        GoShopping.BLL.ProductSuplyBean psb = new GoShopping.BLL.ProductSuplyBean();
        return psb.GetProductSuply(Convert.ToInt32(obj)).Name;
    }
    /// <summary>
    /// 获取打折表
    /// </summary>
    public void GetProList()
    {
        extGVProPrdt.DataSource = new GoShopping.BLL.PrdctInfo.DiscountPrdctBean().GetAll();
        extGVProPrdt.DataBind();
    }
    #endregion

    #region 打折商品设置
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlType.SelectedValue == "-1")
        {
            GetPrdtDataByType(extGVPromotedPrdt, null);
            return;
        }
        string filterStr = "AND ProductTypeID=" + ddlType.SelectedValue;
        GetPrdtDataByType(extGVPromotedPrdt, filterStr);
    }

    protected void extGVPromotedPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (ddlType.SelectedValue == "-1")
        {
            this.extGVPromotedPrdt.PageIndex = e.NewPageIndex;
            GetPrdtDataByType(extGVPromotedPrdt, null);
            return;
        }
        string filterStr = "AND ProductTypeID=" + ddlType.SelectedValue;
        this.extGVPromotedPrdt.PageIndex = e.NewPageIndex;
        GetPrdtDataByType(extGVPromotedPrdt, filterStr);
    }
    /// <summary>
    /// 商品列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void extGVPromotedPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PrdtEdit")
        {
            hidPrdtId.Value = e.CommandArgument.ToString();
            lblName.Text = pb.GetProduct(Convert.ToInt32(e.CommandArgument)).Name;
            hidImgPro.Value = GoShopping.BLL.ProductImgBean.GetProductImgPath(Convert.ToInt32(e.CommandArgument), new GoShopping.BLL.ProductBean().GetProduct(Convert.ToInt32(e.CommandArgument)).ProductTypeID);
        }
        else if (e.CommandName == "PrdtDel")
        {

        }
    }

    protected void btnAddPromoted_Click(object sender, EventArgs e)
    {
        dp.PrdctID = Convert.ToInt32(hidPrdtId.Value);
        dp.DiscountPrice = Convert.ToDecimal(zlTxtOffPrice.Text.Trim());
        dp.StartDate = Convert.ToDateTime(zlTxtStartTime.Text.Trim());
        dp.EndDate = Convert.ToDateTime(zlTxtEndTime.Text.Trim());
        dpb.AddOne(dp);
        GetProList();
    }

    /// <summary>
    /// 折扣商品列表事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void extGVProPrdt_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        extGVProPrdt.PageIndex = e.NewPageIndex;
        GetProList();
    }

    /// <summary>
    /// 打折商品列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void extGVProPrdt_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PrdtDel")
        {
            GoShopping.BLL.PrdctInfo.DiscountPrdctBean dpb = new GoShopping.BLL.PrdctInfo.DiscountPrdctBean();
            dpb.DelDiscountPrdct(Convert.ToInt32(e.CommandArgument));
            GetProList();
        }
    }
    #endregion
}
